import {inIt} from '@/Acc-config/init.js'
// 及时反馈
class Feadback{
	constructor(title,icon="error"){
		this.title = title
		this.icon = icon
	}
	// 消息提示框
	toast(){
		wx.showToast({
		  title: this.title,
		  icon: this.icon,
		  mask:true,
		  duration: 800
		})
	}
	
	
}
// 媒体处理
class Upload{
	constructor(){}
	// 先获取本地图片，再上传到云服务
	// 上传本地图片,默认上传一张
	image(count = 1,type = 'image'){
		// 封装成一个promise
		return new Promise((resole,reject)=>{
			wx.chooseMedia({
			  count: 9,
			  mediaType: [type],
			  sourceType: ['album']
			})
			.then(res=>{
				resole(res.tempFiles)
			})
			.catch(err=>{
				reject(res)
			})
		})
	}
	
	// 上传图片或者视频到云存储
	async cloud(route){
		// 找到最后一个点
		let images = route.lastIndexOf('.');
		// 获取.jpg
		let exing = route.slice(images);
		// 拼接
		let cloudPath = `${Date.now()}-${Math.floor(Math.random(0,1)*10000)}${exing}`;
		let DB =await inIt()
		return new Promise((resolve,reject)=>{
			//作为被接收端 商家端不能用wx.
			DB.uploadFile({
			  cloudPath: 'media/'+cloudPath,
			  filePath: route, // 文件路径
			  success: async(res) => { 
			    // console.log(res)
				const res_url = await DB.getTempFileURL({fileList:[res.fileID]});
				// console.log('222',res_url);
				resolve(res_url.fileList[0].tempFileURL)
			  },
			  fail: err => {
			    // console.log('1111',err)
				reject(err)
			  }
			})
		})
	}
	// 多图上传
	 multi(uploads,key){
		 let storage = []
		return new Promise((resolve,reject)=>{
			uploads.forEach(async item=>{
				let nm = await this.cloud(item.image)
				// 等到上传完成才返回
				storage.push({[key]:nm})
				if(uploads.length ===storage.length){
					resolve(storage)
				}
			})
		})
	}
	
	
	// 预览图片
	preview(image,arr){
		wx.previewImage({
			current:image,//当前显示图片的http链接
			urls: arr//需要预览的图片http链接列表
		})
	}
}
export {Feadback,Upload}